package com.ruoyi.coffee.controller;
import com.ruoyi.coffee.dto.AddressDto;
import com.ruoyi.coffee.service.IAddressesService;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author dxj
 * @Description 用户地址管理
 * @date 2025/1/7 11:34
 */
@RestController
@RequestMapping("/addresses")
@AllArgsConstructor
@Slf4j
@Api(tags = "用户地址")
public class AddressesController {

    private final IAddressesService addressesService;

    @PostMapping("/sava")
    @ApiOperation(value = "用户地址新增", notes = "用户地址新增")
    public AjaxResult saveAddress(@RequestBody AddressDto addressDto) {
        log.info("用户地址新增接口入参{}", addressDto);
        return addressesService.saveUserAddress(addressDto);
    }


    @GetMapping("/list/{userId}")
    @ApiOperation(value = "用户地址查询", notes = "查询用户地址")
    public AjaxResult getAddressesList(@PathVariable Long userId) {
        return addressesService.getAddressesListById(userId);
    }

    @PostMapping("/update")
    @ApiOperation(value = "用户地址修改", notes = "用户地址修改")
    public AjaxResult updateAddress(@RequestBody AddressDto addressDto) {
        log.info("用户地址修改接口入参{}", addressDto);
        return addressesService.updateUserAddress(addressDto);
    }


    @GetMapping("/updateDefaultAddress")
    @ApiOperation(value = "用户默认地址设置", notes = "用户默认地址设置")
    public AjaxResult updateDefaultAddress( @RequestParam Long addressId,@RequestParam Long userId) {
        log.info("用户地址修改接口入参{}", addressId);
        try {
            return addressesService.updateDefaultAddress(addressId,userId);
        } catch (Exception e) {
            log.debug("用户地址修改接口异常{}", e);
        }
        return AjaxResult.error("默认地址设置失败！请重试");
    }
}

